package com.hsmpay.utils.system;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ConConfig {

	private static Map<String, String> map;
	public static Integer debug;
	static {
		ParseConstant();
		debug = Integer.parseInt(getAttribute("DEBUG_STATUS"));
	}

	public static String getAttribute(String key) {
		return map.get(key) != null ? map.get(key) : "";
	}

	@SuppressWarnings("unchecked")
	private static void ParseConstant() {
		String key = "";
		String value = "";
		map = new HashMap<String, String>();
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(ConConfig.class.getClassLoader()
					.getResourceAsStream("config.xml"));
			Element root = document.getRootElement();
			// 遍历根结点root的所有子节点element
			for (Iterator iter = root.elementIterator(); iter.hasNext();) {
				Element element = (Element) iter.next();
				// 遍历结点element的子节点（key，value），并进行处�?
				for (Iterator iterInner = element.elementIterator(); iterInner
						.hasNext();) {
					Element elementInner = (Element) iterInner.next();
					if (elementInner.getName().equals("key")) {
						key = elementInner.getText();
					}
					if (elementInner.getName().equals("value")) {
						value = elementInner.getText();
					}
					if ((!"".equals(key)) && (!"".equals(value)) && (key != null) && (value != null)) {
						map.put(key, value);
						key = null;
						value = null;
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static HashMap<String, String> getDataHashMap(String path) {
		HashMap<String, String> returnValue = new HashMap<String, String>();
		SAXReader saxReader = new SAXReader();
		String key = "";
		String value = "";
		try {
			Document document = saxReader.read(ConConfig.class.getClassLoader().getResourceAsStream(path));
			Element root = document.getRootElement();
			// 遍历根结点root的所有子节点element
			for (Iterator iter = root.elementIterator(); iter.hasNext();) {
				Element element = (Element) iter.next();
				// 遍历结点element的子节点（key，value），并进行处�?
				for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
					Element elementInner = (Element) iterInner.next();
					if (elementInner.getName().equals("key")) {
						key = elementInner.getText();
					}
					if (elementInner.getName().equals("value")) {
						value = elementInner.getText();
					}
					if ((!"".equals(key)) && (!"".equals(value)) && (key != null) && (value != null)) {
						System.out.println("key" + key+":"+value);
						returnValue.put(key, value);
						key = null;
						value = null;
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return returnValue;

	}

	public static boolean isFormal(){//是否为生产环境
		return 1 == ConConfig.debug;
	}
	
	public static void main(String[] args) {
		//String path = "com/wanjx/mobile/util/system/constant.xml";
		//HashMap<String, String> map = getDataHashMap(path);
		//System.out.println("map:" + map.entrySet());
		System.out.println("DEBUG_STATUS:" +debug);
		System.out.println("DEBUG_STATUS:" +getAttribute("DEBUG_STATUS"));
	}
}
